package org.association;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.association.dao.OrganizationDao;
import org.association.entity.OrganizationEntity;
import org.association.service.AddressService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.*;

@SpringBootApplication
@MapperScan(basePackages = {"org.association.dao"})
public class AssociationApplication {

    private static String imageDir = "/root/imgs/img";
//    private static String imageDir = "C:\\Users\\froggynie\\Desktop\\img(1)\\img";
    private static String ip = "127.0.0.1";

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext =
                SpringApplication.run(AssociationApplication.class, args);

        // 插入地址数据
        AddressService addressService = applicationContext.getBean(AddressService.class);
        List<String> addresses = readAddress();
        addressService.initAddress(addresses);

        // 插入社团数据
        OrganizationDao organizationDao = applicationContext.getBean(OrganizationDao.class);
        loadOrganization(organizationDao, addressService);
    }

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        return new RestTemplate(factory);
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(150000); // ms
        factory.setConnectTimeout(150000); // ms
        return factory;
    }

    public static List<String> readAddress() {
        List<String> address = new ArrayList<>();
//        String fileName = "C:\\Users\\froggynie\\Desktop\\address.txt";
//        try (Scanner sc = new Scanner(new FileReader(fileName))) {
//            while (sc.hasNextLine()) {  //按行读取字符串
//                String line = sc.nextLine();
//                System.out.println("address.add(\"" + line + "\");");
//                address.add(line.trim());
//            }
//        }
        address.add("0x5b932494f81ea3f929b1d36cd5a96e168a92bb53");
        address.add("0x5eb449663b728462ef395389a957e27c91839cb4");
        address.add("0x6a150da948520382187de1810bab1bf9f6704569");
        address.add("0x1fad210615b953bb647312eb643480fbc1ea171e");
        address.add("0x36327db3ca28bbea010396c98686961d7d7b92cb");
        address.add("0x25e8767bb0a7abed7f055c07d71738fa5ad90604");
        address.add("0x2610cdc6a220068b02618aa8aabe5047689519dd");
        address.add("0x8dc75e2180b2bd5377ef482d66257b3b0aa29ced");
        address.add("0xbd175a96e10fcbd044f994f4732892580e8e8e43");
        address.add("0xae378644afbb5ebcf4d0c082097dcb711fe59580");
        address.add("0x74a1cafdbd475c39cba61d86c017f2d9cccb3655");
        address.add("0x025251ec72f40fb8d9bbdc2d92dc952755f1f49d");
        address.add("0x6f74ffb617e10e0779367bdeca53b82d5a9e2355");
        address.add("0xdae30f17d805137d216a954f4a352628edaa8d9b");
        address.add("0x2838a0e9c0189a0f22b698ab6f66c01e531cdbfc");
        address.add("0xaef60a322e59feeee4bea1edc1f85b58102f2b6c");
        address.add("0x1c22a1d9abfdbe4b40ac9c95ea13344db22b1566");
        address.add("0x3ed6e7394bd83ac412c27fb6975ea9fbc5522c7a");
        address.add("0x6d89861532ad60ea2966dc1a9587097976ddb216");
        address.add("0x2398f35bc63f3023bc45b97ac1b73831b8f4898d");
        address.add("0x3cf67fd849d919c15412875fc3b12a6a756cba88");
        address.add("0xf8f90343abc90f45307ffa96d2f80019c86c4ad3");
        address.add("0x6f4265d029c5780b73af7783fa36f36ad55f6490");
        address.add("0x370afb9bad9987fa9368dc3e84b25ae2807d67e8");
        address.add("0x820a222df985fcc1e5ad4dfefa329b74094e5a8c");
        address.add("0x35cb396c38e25f7d56fa773a1062c1a05a78e9ba");
        address.add("0x92217ebf950ac48f1a08f46745c6cc265f369105");
        address.add("0x01f330a2cc7955f52334d84dbf41662416ad23bf");
        address.add("0x01c87067827fd6fc89bf3f0ff1a6a6bdc0c33cd6");
        address.add("0x51220608ea3f1741471ffd0bc690a843f7e3e1ec");
        address.add("0xf323b220129d436773178d3d4a6242a84a45d1c7");
        address.add("0xb92fbd50aa2d9c03f492d0c268f1141017bfd481");
        address.add("0x5390200b3ee1a9d5c818a754083147af80553756");
        address.add("0x79497cae3bdc96076e9d3c98d416b42faf186a39");
        address.add("0x004996b7c09f0ca85f676c55991d8ec7e92f3f75");
        address.add("0x833bfefd4167692f190bc1490dcaf5e7437487c6");
        address.add("0x7cf193e8b1f118b6707f1dfbaa8568ffeee26adf");
        address.add("0xc3fdb26b1c16206a9eeea43491a132ba56785079");
        address.add("0x940f64048bf8d5d2252f4c5bb0f5c63e4e0190bf");
        address.add("0x57a12ec3a4b095a7454c7ebd33aae6d294522f4f");
        address.add("0xb391a49bd156a0450f30854297844214a6961d38");
        address.add("0x5cbc9b9dd2264a386e82d57d0714b47e87e085b6");
        address.add("0x59234b4df0828e206a690dd0d12551154ae5c758");
        return address;
    }

    /**
     * 写入协会数据到数据库
     */
    private static void loadOrganization(OrganizationDao organizationDao, AddressService addressService) {

        Long count = organizationDao.selectCount(new QueryWrapper<>());
        if(count != 0) {
            return;
        }

        // 删除所有组织信息
        organizationDao.delete(new QueryWrapper<>());

        // 创建一个HashMap实例
        HashMap<String, String> hashMap = new HashMap<>();

        // 向HashMap中添加键值对
        hashMap.put("yinyue", "音乐协会");
        hashMap.put("pingpangqiu", "乒乓球协会");
        hashMap.put("tubu", "徒步协会");
        hashMap.put("sheji", "设计协会");
        hashMap.put("zhuoyou", "桌游协会");
        hashMap.put("wangqiu", "网球协会");
        hashMap.put("lanqiu", "篮球协会");
        hashMap.put("youyong", "游泳协会");
        hashMap.put("zhongpao", "众跑协会");
        hashMap.put("gonghui", "工会");
        hashMap.put("sheying", "摄影协会");
        hashMap.put("shuhua", "书画协会");
        hashMap.put("zuqiu", "足球协会");
        hashMap.put("wudao", "舞蹈协会");
        hashMap.put("yumaoqiu", "羽毛球协会");
        String url = imageDir;
        File file = new File(url);

        if(file == null || !file.exists()) {
            return;
        }

        File[] files = file.listFiles();
        int i= 1;
        for (File file1 : files) {
            System.out.println(i++);
            String name = file1.getName();
            OrganizationEntity organizationEntity = new OrganizationEntity();
            organizationEntity.setName(hashMap.get(name.replace(".png", "")));
            organizationEntity.setIp(ip);
            organizationEntity.setPort(8088);
            organizationEntity.setPath("/static/img/" + name);

            // 分配地址
            String address = addressService.allocateAddress();
            organizationEntity.setAddress(address);

            // 写入数据库
            organizationDao.insert(organizationEntity);
            addressService.allocate(address);
        }
    }

}
